package org.jeecg.modules.hotel.requisition.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.dto.message.BusMessageDTO;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.SysAnnmentTypeEnum;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.demo.apartment.entity.LwgyBaseApartment;
import org.jeecg.modules.basic.demo.apartment.mapper.LwgyBaseApartmentMapper;
import org.jeecg.modules.basic.demo.bed.entity.LwgyBaseBed;
import org.jeecg.modules.basic.demo.bed.mapper.LwgyBaseBedMapper;
import org.jeecg.modules.basic.demo.bed.service.ILwgyBaseBedService;
import org.jeecg.modules.basic.demo.student.entity.LwgyBaseStudent;
import org.jeecg.modules.basic.demo.student.mapper.LwgyBaseStudentMapper;
import org.jeecg.modules.basic.demo.student.service.ILwgyBaseStudentService;
import org.jeecg.modules.hotel.arrangement.controller.LwgyHotelArrangementController;
import org.jeecg.modules.hotel.arrangement.controller.LwgyHotelArrangementHolidayController;
import org.jeecg.modules.hotel.arrangement.entity.LwgyHotelArrangement;
import org.jeecg.modules.hotel.arrangement.mapper.LwgyHotelArrangementMapper;
import org.jeecg.modules.hotel.arrangement.service.ILwgyHotelArrangementService;
import org.jeecg.modules.hotel.requisition.entity.LwgyHotelRequisition;
import org.jeecg.modules.hotel.requisition.entity.StudentRequest;
import org.jeecg.modules.hotel.requisition.service.ILwgyHotelRequisitionService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 调退宿申请
 * @Author: jeecg-boot
 * @Date:   2021-05-04
 * @Version: V1.0
 */
@Api(tags="调退宿申请")
@RestController
@RequestMapping("/requisition/lwgyHotelRequisition")
@Slf4j
public class LwgyHotelRequisitionController extends JeecgController<LwgyHotelRequisition, ILwgyHotelRequisitionService> {
	@Autowired
	private ILwgyHotelRequisitionService lwgyHotelRequisitionService;

	@Resource
	private LwgyBaseBedMapper lwgyBaseBedMapper;

	@Resource
	private LwgyBaseStudentMapper lwgyBaseStudentMapper;

	@Resource
	private LwgyBaseApartmentMapper lwgyBaseApartmentMapper;

	@Autowired
	private ILwgyBaseBedService lwgyBaseBedService;

	@Autowired
	private ILwgyBaseStudentService lwgyBaseStudentService;

	@Autowired
	private ISysBaseAPI sysBaseAPI;

	@Autowired
	private LwgyHotelArrangementController lwgyHotelArrangementController;

	 @Autowired
	 private LwgyHotelArrangementHolidayController lwgyHotelArrangementHolidayController;

	 @Autowired
	 private  ILwgyHotelRequisitionService ILwgyHotelRequisitionService;
	/**
	 * 分页列表查询
	 *
	 * @param lwgyHotelRequisition
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "调退宿申请-分页列表查询")
	@ApiOperation(value="调退宿申请-分页列表查询", notes="调退宿申请-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(LwgyHotelRequisition lwgyHotelRequisition,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LwgyHotelRequisition> queryWrapper = QueryGenerator.initQueryWrapper(lwgyHotelRequisition, req.getParameterMap());
         		Page<LwgyHotelRequisition> page = new Page<>(pageNo, pageSize);
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
		//管理员不限制
		//辅导员只看他自己学生申请
		//学生只看他提交的
		List<Map<String,String>> fdyList = null;
		if(roles.contains("fdy")){
			List<String> classId = lwgyBaseStudentService.getClassIdBySysUser(sysUser);
			if (oConvertUtils.listIsNotEmpty((classId))) {
				queryWrapper.in("student_class", classId);
			} else {
				queryWrapper.eq("student_class", "null");
			}
	/*		queryWrapper.in("admin_id",sysUser.getWorkNo());*/
		}else if(roles.contains("user_students")){
			queryWrapper.eq("student_number",sysUser.getWorkNo());
		}

		IPage<LwgyHotelRequisition> pageList = lwgyHotelRequisitionService.page(page, queryWrapper);

		return Result.OK(pageList);
	}
	/**
	 *   添加
	 *
	 * @param lwgyHotelRequisition
	 * @return
	 */
	@AutoLog(value = "调退宿申请-添加")
	@ApiOperation(value="调退宿申请-添加", notes="调退宿申请-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody LwgyHotelRequisition lwgyHotelRequisition) {
//		LwgyBaseStudent lwgyBaseStudent = lwgyBaseStudentService.getById(sysUser.getId());
//		lwgyHotelRequisitionService.pushMsg(lwgyHotelRequisition, username, roles, lwgyBaseStudent);
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
		if (roles.contains("user_students") && !lwgyHotelRequisition.getStudentNumber().equals(sysUser.getWorkNo())) {
			return Result.error("学号不匹配！");
		}

		LwgyBaseStudent lwgyBaseStudent =lwgyBaseStudentMapper.getStudentByNumber(lwgyHotelRequisition.getStudentNumber());
		if(oConvertUtils.isNotEmpty(lwgyBaseStudent)){
			lwgyHotelRequisition.setStudentId(lwgyBaseStudent.getId());
			lwgyHotelRequisition.setStudentCampus(lwgyBaseStudent.getStudentCampus());
			lwgyHotelRequisition.setStudentCollege(lwgyBaseStudent.getStudentCollege());
			lwgyHotelRequisition.setStudentMajor(lwgyBaseStudent.getStudentMajor());
			lwgyHotelRequisition.setStudentYear(lwgyBaseStudent.getStudentYear());
			lwgyHotelRequisition.setStudentClass(lwgyBaseStudent.getStudentClass());
		}

		LwgyBaseBed lwgyBaseBed = lwgyBaseBedMapper.getBedByStuId(lwgyBaseStudent.getId());
		if(oConvertUtils.isNotEmpty(lwgyBaseBed)){
			lwgyHotelRequisition.setBuildingId(lwgyBaseBed.getBuildingId());
			lwgyHotelRequisition.setUnitId(lwgyBaseBed.getUnitId());
			lwgyHotelRequisition.setLayerId(lwgyBaseBed.getLayerId());
			lwgyHotelRequisition.setRoomId(lwgyBaseBed.getRoomId());
		}

		String username = sysUser.getUsername();
		lwgyHotelRequisition.setFromUser(username);
		lwgyHotelRequisitionService.save(lwgyHotelRequisition);
		return Result.OK("申请成功！");
	}

	 /**
	 *  编辑
	 *
	 * @param lwgyHotelRequisition
	 * @return
	 */
	 @AutoLog(value = "调退宿申请-编辑")
	 @ApiOperation(value = "调退宿申请-编辑", notes = "调退宿申请-编辑")
	 @PutMapping(value = "/edit")
	 public Result<?> edit(@RequestBody LwgyHotelRequisition lwgyHotelRequisition) {
		 String processingResult = lwgyHotelRequisition.getProcessingResult();
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String username = sysUser.getUsername();
		 String stuUsername = oConvertUtils.isEmpty(lwgyHotelRequisition.getFromUser()) ? "admin" : lwgyHotelRequisition.getFromUser();
		 if (oConvertUtils.isNotEmpty(processingResult) && "0".equals(processingResult)) {
			 sysBaseAPI.sendSysAnnouncement(new MessageDTO(username, stuUsername, "申请回执", "管理员拒绝了你的申请：原因如下->" + lwgyHotelRequisition.getRefuseReason(), "1"));
			 lwgyHotelRequisition.setRefuseReason(lwgyHotelRequisition.getRefuseReason());
			 System.out.println("========== 保存成功 ==========");
		 } else if ("1".equals(processingResult)) {//同意
			 String behavioral = lwgyHotelRequisition.getBehavioral();//获取行为
			 String studentId = lwgyHotelRequisition.getStudentId();//获取学生id
			 LwgyHotelRequisition lwgyHotel = lwgyHotelRequisitionService.getStuAdmin(studentId);
			 LwgyHotelArrangement lwgyHotelArrangement = new LwgyHotelArrangement();
			 lwgyHotelArrangement.setStudentId(studentId);//
			 lwgyHotelArrangement.setBehavior(behavioral);//
//			lwgyBaseBedService.getOne()
			 QueryWrapper<LwgyBaseBed> queryWrapper = new QueryWrapper<>();
			 queryWrapper.select("id").eq("student_id", studentId);
			 List<LwgyBaseBed> lwgyBaseBeds = lwgyBaseBedMapper.selectList(queryWrapper);//查找该学生所有床位
//			String bedId = lwgyHotelRequisition.getBedId();
			 String afterBed = lwgyHotelRequisition.getAfterBed();
			 String beforeBed = lwgyHotelRequisition.getBeforeBed();
			 String content = "";
			 if ("leave_bed".equals(behavioral)) {//如果退宿
				 if (lwgyBaseBeds == null || lwgyBaseBeds.size() <= 0) {//没有住宿信息，无需退宿
					 content = "系统中没有住宿信息，无需退宿";
					 lwgyHotelRequisition.setProcessingResult("0");
					 sysBaseAPI.sendSysAnnouncement(new MessageDTO(username, stuUsername, "申请回执", content, "1"));

				 } else {
					 lwgyHotelRequisition.setProcessingResult("1");
					 lwgyHotelRequisition.setDelFlag("0");
					 LwgyBaseBed lwgyBaseBed = lwgyBaseBeds.get(0);
					 lwgyHotelArrangement.setBeforeBed(lwgyBaseBed.getId());
					 //					lwgyHotelArrangement.setBeforeBed(beforeBed);
					 lwgyHotelArrangementController.checkOutStu(lwgyHotelArrangement);
					 content = "管理员同意了你的退宿申请";
					 sysBaseAPI.sendSysAnnouncement(new MessageDTO(username, stuUsername, "申请回执", content, "1"));
					 sendSysToAdmin(lwgyBaseBed, "退宿", "", username, stuUsername);
				 }
			 } else if ("choose_bed".equals(behavioral)) {//入住
				 if (lwgyBaseBeds != null && lwgyBaseBeds.size() > 0) {//系统中有住宿信息，不能重复入住
					 content = "系统中已经有住宿信息，请申请退宿、或调宿";
					 lwgyHotelRequisition.setProcessingResult("0");
					 sysBaseAPI.sendSysAnnouncement(new MessageDTO(username, stuUsername, "申请回执", content, "1"));
				 } else {
					 lwgyHotelRequisition.setProcessingResult("1");
					 lwgyHotelRequisition.setDelFlag("0");
					 lwgyHotelArrangement.setAdminId(lwgyHotel.getAdminName());
					 lwgyHotelArrangement.setAfterBed(afterBed);//入住后的床位
					 lwgyHotelArrangementController.updateBedStu(lwgyHotelArrangement);
					 LwgyBaseBed lwgyBaseBed = lwgyBaseBedMapper.queryBedById(afterBed);
					 String bedCode = lwgyBaseBed.getBedCode();
					 content = "管理员同意了你的入住申请，:已为你安排到：" + lwgyBaseBed.getCampusName() + "-" + lwgyBaseBed.getBuildingName()
							 + "-" + lwgyBaseBed.getLayerName()
							 + "-" + lwgyBaseBed.getRoomName() + "-" + bedCode.substring(bedCode.length() - 2) + "号床位，床位价格为：" + lwgyBaseBed.getBedPrice();
					 sysBaseAPI.sendSysAnnouncement(new MessageDTO(username, stuUsername, "申请回执", content, "1"));
					 sendSysToAdmin(lwgyBaseBed, "入住", bedCode, username, stuUsername);
				 }
			 } else if ("change_bed".equals(behavioral)) {//调宿
				 if (oConvertUtils.listIsEmpty(lwgyBaseBeds)) {//系统中没有住宿信息，没法调宿
					 content = "系统中没有住宿信息，不能完成调宿申请";
					 lwgyHotelRequisition.setProcessingResult("0");
					 sysBaseAPI.sendSysAnnouncement(new MessageDTO(username, stuUsername, "申请回执", content, "1"));
				 } else {
					 lwgyHotelRequisition.setProcessingResult("1");
					 lwgyHotelRequisition.setDelFlag("0");
					 LwgyBaseBed lwgyBaseBed0 = lwgyBaseBeds.get(0);
					 lwgyHotelArrangement.setBeforeBed(lwgyBaseBed0.getId());
					 lwgyHotelArrangement.setAfterBed(afterBed);
					 lwgyHotelArrangement.setAdminId(lwgyHotel.getAdminName());
					 lwgyHotelArrangementController.updateBedStu(lwgyHotelArrangement);
					 LwgyBaseBed lwgyBaseBed = lwgyBaseBedMapper.queryBedById(afterBed);
					 String bedCode = lwgyBaseBed.getBedCode();
					 content = lwgyBaseBed.getCampusName() + "-" + lwgyBaseBed.getBuildingName()
							 + "-" + lwgyBaseBed.getLayerName()
							 + "-" + lwgyBaseBed.getRoomName() + "-" + bedCode.substring(bedCode.length() - 2) + "号床位，新床位价格为：" + lwgyBaseBed.getBedPrice() + "(多退少补)";
					 sysBaseAPI.sendSysAnnouncement(new MessageDTO(username, stuUsername, "申请回执", "管理员同意了你的调宿申请，已为你安排到：" + content, "1"));
					 sendSysToAdmin(lwgyBaseBed, "调宿舍", bedCode, username, stuUsername);
				 }
			 } else if ("hoilday_bed".equals(behavioral)) {//假期入住
				 lwgyHotelRequisition.setProcessingResult("1");
				 lwgyHotelRequisition.setDelFlag("0");
				 //查找之前是否有假期住宿的床位
				 QueryWrapper<LwgyBaseBed> queryWrapper2 = new QueryWrapper<>();
				 queryWrapper2.eq("holiday_student_id", lwgyHotelRequisition.getStudentId());
				 List<LwgyBaseBed> lwgyBaseBedss = lwgyBaseBedMapper.selectList(queryWrapper2);//查找该学生原是否有假期床位
				 if (lwgyBaseBedss.size() > 0) {
					 //先退宿再入住
					 for (LwgyBaseBed lbb : lwgyBaseBedss) {
						 lbb.setHolidayStudentId("");
						 lwgyBaseBedService.updateById(lbb);
					 }

					 afterBed = lwgyHotelRequisition.getAfterBed();
					 LwgyBaseBed lwgyBaseBed = lwgyBaseBedMapper.queryBedById(afterBed);
					 lwgyBaseBed.setHolidayStudentId(lwgyHotelRequisition.getStudentId());
					 lwgyBaseBedService.updateById(lwgyBaseBed);
					 String bedCode = lwgyBaseBed.getBedCode();

					 content = "管理员已经将你的假期床位调整到：" + lwgyBaseBed.getCampusName() + "-" + lwgyBaseBed.getBuildingName()
							 + "-" + lwgyBaseBed.getLayerName()
							 + "-" + lwgyBaseBed.getRoomName() + "-" + bedCode.substring(bedCode.length() - 2) + "号床位，床位价格为：" + lwgyBaseBed.getBedPrice();
					 sysBaseAPI.sendSysAnnouncement(new MessageDTO(username, stuUsername, "申请回执", content, "1"));
					 sendSysToAdmin(lwgyBaseBed, "假期床位调整", bedCode, username, stuUsername);
				 } else {
					 afterBed = lwgyHotelRequisition.getAfterBed();
					 LwgyBaseBed lwgyBaseBed = lwgyBaseBedMapper.queryBedById(afterBed);
					 lwgyBaseBed.setHolidayStudentId(lwgyHotelRequisition.getStudentId());
					 lwgyBaseBedService.updateById(lwgyBaseBed);
					 String bedCode = lwgyBaseBed.getBedCode();
					 content = "管理员同意了你的假期入住申请，:已为你安排到：" + lwgyBaseBed.getCampusName() + "-" + lwgyBaseBed.getBuildingName()
							 + "-" + lwgyBaseBed.getLayerName() + "-" + lwgyBaseBed.getRoomName() + "-"
							 + bedCode.substring(bedCode.length() - 2) + "号床位，床位价格为：" + lwgyBaseBed.getBedPrice();
					 sysBaseAPI.sendSysAnnouncement(new MessageDTO(username, stuUsername, "申请回执", content, "1"));
					 sendSysToAdmin(lwgyBaseBed, "假期入住", bedCode, username, stuUsername);

				 }
			 }
			 lwgyHotelRequisition.setRefuseReason(content);
		 }
		 lwgyHotelRequisitionService.updateById(lwgyHotelRequisition);
		 return Result.OK("编辑成功!");
	 }

	public void sendSysToAdmin(LwgyBaseBed lwgyBaseBed ,String  type,String bedCode,String username,String stuUsername){
		String content="";
		if(type.equals("退宿")){
			content = "已同意学生"+username+"的"+type+"申请.";
		}else{
			content = "已同意学生"+username+"的"+type+"申请，:为他安排到："+lwgyBaseBed.getCampusName()+"-"+lwgyBaseBed.getBuildingName()
					+"-"+lwgyBaseBed.getLayerName()
					+"-"+lwgyBaseBed.getRoomName()+"-"+bedCode.substring(bedCode.length()-2)+"号床位。";
		}
		sysBaseAPI.sendSysAnnouncement(new MessageDTO("skdadmin",stuUsername,"事务申请操作通知",content,"1"));

	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "调退宿申请-通过id删除")
	@ApiOperation(value="调退宿申请-通过id删除", notes="调退宿申请-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		lwgyHotelRequisitionService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "调退宿申请-批量删除")
	@ApiOperation(value="调退宿申请-批量删除", notes="调退宿申请-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.lwgyHotelRequisitionService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "调退宿申请-通过id查询")
	@ApiOperation(value="调退宿申请-通过id查询", notes="调退宿申请-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LwgyHotelRequisition lwgyHotelRequisition = lwgyHotelRequisitionService.getById(id);
		if(lwgyHotelRequisition==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(lwgyHotelRequisition);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param lwgyHotelRequisition

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LwgyHotelRequisition lwgyHotelRequisition) {
        return super.exportXls(request, lwgyHotelRequisition, LwgyHotelRequisition.class, "调退宿申请");
    }*/

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LwgyHotelRequisition.class);
    }
	 /**
	  *查询所有学校
	  *
	  *
	  */
	 @AutoLog(value = "学校信息-通过pid查询")
	 @ApiOperation(value="学校信息-通过pid查询", notes="学校信息-通过pid查询")
	 @GetMapping(value = "/getSchoolData")
	 public Result<?> getSchoolData(@RequestParam(name="pid") String pid){
		 System.out.println("pid ------>>>> " + pid);
		 List<Map<String,String>> maps = ILwgyHotelRequisitionService.listByPids(pid);
		 return Result.OK(maps);
	 }
	 /**
	  * 查询学生基础信息
	  *
	  *
	  */
	 @AutoLog(value = "学生信息-通过学号查询")
	 @ApiOperation(value = "学生信息-通过学号查询",notes="学生信息-通过学号查询")
	 @GetMapping(value = "/getStuInfo")
	 public Result<?> getStuInfo(@RequestParam(name = "studentNumber")String studentNumber){
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());

		 if (roles.contains("admin") || roles.contains("fdy")) {
			 List<Map<String, String>> maps = ILwgyHotelRequisitionService.listByStuNum(studentNumber);
			 if (maps == null || maps.isEmpty()) {
				 return Result.error(404,"学生不存在");
			 }
			 return Result.OK(maps);
		 } else if (roles.contains("user_students")) {
			 if (!sysUser.getUsername().equals(studentNumber)) {
				 return Result.error(403,"帐号不匹配");
			 }
			 // 查询学生信息
			 List<Map<String, String>> maps = ILwgyHotelRequisitionService.listByStuNum(studentNumber);
			 return Result.OK(maps);
		 } else {
			 // 其他角色无权限访问
			 return Result.error(500,"无权限访问");
		 }
	 }

	 /**
	  *查询楼宇上该楼宇上学生的事务申请
	  *
	  *
	  */
	 @AutoLog(value = "查询楼宇上该楼宇上学生的事务申请")
	 @ApiOperation(value="查询楼宇上该楼宇上学生的事务申请", notes="查询楼宇上该楼宇上学生的事务申请")
	 @GetMapping(value = "/getStudentRequisition")
	 public Result<?> getStudentRequisition(String id){
		 QueryWrapper<LwgyBaseApartment> queryWrapper = new QueryWrapper<>();
		 queryWrapper.select("id","type");
		 queryWrapper.eq("id",id);
		 LwgyBaseApartment lwgyBaseApartment = lwgyBaseApartmentMapper.selectOne(queryWrapper);
		 if(oConvertUtils.isEmpty(lwgyBaseApartment)){
		 	return Result.error("所选内容不存在");
		 }
		 List<StudentRequest> studentRequests = lwgyHotelRequisitionService.getStudentRequisition(lwgyBaseApartment);
		 return Result.OK(studentRequests);
	 }
	 @AutoLog(value = "楼宇信息-通过pid查询")
	 @ApiOperation(value="楼宇信息-通过pid查询", notes="楼宇信息-通过pid查询")
	 @GetMapping(value = "/getbuildingData")
	 public Result<?> getbuildingData(@RequestParam(name="pid") String pid){
		 System.out.println("pid ------>>>> " + pid);
		 List<Map<String,String>> maps = ILwgyHotelRequisitionService.listByPides(pid);
		 return Result.OK(maps);
	 }

	 /**
	  *  编辑
	  *
	  * @param lwgyHotelRequisition
	  * @return
	  */
	 @AutoLog(value = "调宿申请-交换宿舍")
	 @ApiOperation(value="调宿申请-交换宿舍", notes="调宿申请-交换宿舍")
	 @PutMapping(value = "/swapBed")
	 public Result<?> swapBed(@RequestBody LwgyHotelRequisition lwgyHotelRequisition) {
		 // 获取处理状态
		 String processingResult = lwgyHotelRequisition.getProcessingState();
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String username = sysUser.getUsername();
		 String stuUsername = oConvertUtils.isEmpty(lwgyHotelRequisition.getFromUser()) ? "admin":lwgyHotelRequisition.getFromUser();

		 if(oConvertUtils.isNotEmpty(processingResult) && "0".equals(processingResult)){
			 sysBaseAPI.sendSysAnnouncement(new MessageDTO(username,stuUsername,"申请回执","管理员拒绝了你的申请：原因如下->"+lwgyHotelRequisition.getRefuseReason(),"1"));
		 }else if("1".equals(processingResult)){//同意

			 String behavioral = lwgyHotelRequisition.getBehavioral();//获取行为
			 String studentId = lwgyHotelRequisition.getStudentId();//获取学生id

			 LwgyHotelArrangement lwgyHotelArrangement = new LwgyHotelArrangement();
			 lwgyHotelArrangement.setStudentId(studentId);
			 lwgyHotelArrangement.setBehavior(behavioral);
			 QueryWrapper<LwgyBaseBed> queryWrapper = new QueryWrapper<>();
			 queryWrapper.select("id").eq("student_id",studentId);
			 List<LwgyBaseBed> lwgyBaseBeds = lwgyBaseBedMapper.selectList(queryWrapper);//查找该学生所有床位

			 String content = "";
			 if("change_bed".equals(behavioral)){//如果调宿
				 if(oConvertUtils.listIsEmpty(lwgyBaseBeds)){//系统中没有住宿信息，没法调宿
					 content = "系统中没有住宿信息，不能完成调宿申请";
					 lwgyHotelRequisition.setProcessingResult("0");
					 sysBaseAPI.sendSysAnnouncement(new MessageDTO(username,stuUsername,"申请回执",content,"1"));
				 }else{
					 lwgyHotelRequisition.setProcessingResult("1");
					 lwgyHotelRequisition.setDelFlag("0");
					 LwgyBaseBed lwgyBaseBed = lwgyBaseBedMapper.queryBedByStuId(studentId);
					 String bedCode = lwgyBaseBed.getBedCode();
					 content = lwgyBaseBed.getCampusName()+"-"+lwgyBaseBed.getBuildingName()
							 +"-"+lwgyBaseBed.getLayerName()
							 +"-"+lwgyBaseBed.getRoomName()+"-"+bedCode.substring(bedCode.length()-2)+"号床位，新床位价格为："+lwgyBaseBed.getBedPrice()+"(多退少补)";
					 sysBaseAPI.sendSysAnnouncement(new MessageDTO(username,stuUsername,"申请回执","管理员同意了你的调宿申请，已为你安排到："+content,"1"));
					 sendSysToAdmin(lwgyBaseBed,"调宿舍",bedCode,username,stuUsername);
				 }
			 }
			 lwgyHotelRequisition.setRefuseReason(content);
		 }
		 lwgyHotelRequisitionService.updateById(lwgyHotelRequisition);
		 return Result.OK("编辑成功!");
	 }

	 @RequestMapping(value = "/exportXls")
	 public ModelAndView exportXls(HttpServletRequest request, LwgyHotelRequisition lwgyHotelRequisition) {
		 String title="调退宿申请";
		 // Step.1 组装查询条件
		 QueryWrapper<LwgyHotelRequisition> queryWrapper = QueryGenerator.initQueryWrapper(lwgyHotelRequisition, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 List<LwgyHotelRequisition> exportList = lwgyHotelRequisitionService.list( queryWrapper);
		 exportList = exportList.stream().filter(entity -> entity.getBehavioral() != null && !entity.getBehavioral().isEmpty())
				 .collect(Collectors.toList());
		 // Step.2	遍历查询结果列表，处理每个实体对象
		 for (LwgyHotelRequisition entity : exportList) {
			 // Step.3 获取需要转换的字段值，并根据字段值进行相应的转换
			 String behaveValue = entity.getBehavioral();
			 String behaveText = getBehaveText(behaveValue);

			 // Step.4 将转换后的文本值设置到对应的属性中
			 entity.setBehavioral(behaveText);
		 }
		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 mv.addObject(NormalExcelConstants.FILE_NAME, title); //此处设置的filename无效 ,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.CLASS,  LwgyHotelRequisition.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
	 }
	 private String getBehaveText(String behaveValue) {
		 String behaveText;
		 switch (behaveValue) {
			 case "choose_bed":
				 behaveText = "入住";
				 break;
			 case "change_bed":
				 behaveText = "调宿";
				 break;
			 case "leave_bed":
				 behaveText = "退宿";
				 break;
			 case "hoilday_bed":
				 behaveText = "假期";
				 break;
			 default:
				 throw new IllegalStateException("Unexpected value: " + behaveValue);
		 }
		 return behaveText;
	 }
}
